package com.farmer.knowledge.controller;

import com.farmer.common.entity.AgriKnowledge;
import com.farmer.common.result.Result;
import com.farmer.knowledge.service.KnowledgeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 农技知识库控制器
 */
@Slf4j
@RestController
@RequestMapping("/knowledge")
@Tag(name = "农技知识库管理", description = "农技知识库相关接口")
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    /**
     * 获取知识库列表（分页）
     */
    @GetMapping("/list")
    @Operation(summary = "获取知识库列表", description = "分页获取农技知识库列表")
    public Result<?> getKnowledgeList(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                     @RequestParam(required = false) String category,
                                     @RequestParam(required = false) String keyword) {
        return knowledgeService.getKnowledgeList(pageNum, pageSize, category, keyword);
    }

    /**
     * 根据ID获取知识详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取知识详情", description = "根据ID获取农技知识详细信息")
    public Result<?> getKnowledgeById(@PathVariable Long id) {
        return knowledgeService.getKnowledgeById(id);
    }

    /**
     * 添加知识
     */
    @PostMapping("/add")
    @Operation(summary = "添加知识", description = "新增农技知识")
    public Result<?> addKnowledge(@RequestBody AgriKnowledge knowledge) {
        return knowledgeService.addKnowledge(knowledge);
    }

    /**
     * 更新知识
     */
    @PutMapping("/update")
    @Operation(summary = "更新知识", description = "修改农技知识信息")
    public Result<?> updateKnowledge(@RequestBody AgriKnowledge knowledge) {
        return knowledgeService.updateKnowledge(knowledge);
    }

    /**
     * 删除知识
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除知识", description = "删除指定的农技知识")
    public Result<?> deleteKnowledge(@PathVariable Long id) {
        return knowledgeService.deleteKnowledge(id);
    }

    /**
     * 获取推荐知识列表
     */
    @GetMapping("/featured")
    @Operation(summary = "获取推荐知识", description = "获取推荐的农技知识列表")
    public Result<?> getFeaturedKnowledgeList(@RequestParam(defaultValue = "10") Integer limit) {
        return knowledgeService.getFeaturedKnowledgeList(limit);
    }

    /**
     * 获取知识分类列表
     */
    @GetMapping("/categories")
    @Operation(summary = "获取知识分类", description = "获取所有知识分类")
    public Result<?> getCategoryList() {
        return knowledgeService.getCategoryList();
    }

    /**
     * 增加浏览次数
     */
    @PostMapping("/view/{id}")
    @Operation(summary = "增加浏览次数", description = "增加知识的浏览次数")
    public Result<?> increaseViewCount(@PathVariable Long id) {
        return knowledgeService.increaseViewCount(id);
    }

    /**
     * 点赞/取消点赞
     */
    @PostMapping("/like/{id}")
    @Operation(summary = "点赞操作", description = "对知识进行点赞或取消点赞")
    public Result<?> toggleLike(@PathVariable Long id, @RequestParam Boolean isLike) {
        return knowledgeService.toggleLike(id, isLike);
    }

    /**
     * 发布/下架知识
     */
    @PutMapping("/status/{id}")
    @Operation(summary = "更新知识状态", description = "发布或下架知识")
    public Result<?> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        return knowledgeService.updateStatus(id, status);
    }
} 